import province from './山东省-province.json';
import city from './山东省-city.json';
import county from './山东省-county.json';
// import province from './新疆维吾尔自治区-1.json';
// import city from './新疆维吾尔自治区-2.json';
// import county from './新疆维吾尔自治区-3.json';

export function getGeoJsonByCode(adcode) {
    const lastFour = String(adcode).slice(-4);
    const lastTwo = String(adcode).slice(-2);
    let jsonData;

    //省
    if (lastFour === '0000') {
        return province;
    }
    //市
    else if (lastFour != '0000' && lastTwo === '00') {
        city.features.some((item) => {
            if (item.properties.adcode == adcode) {
                jsonData = item;
                return true;
            }
        });
    }
    //县
    else {
        county.features.some((item) => {
            if (item.properties.adcode == adcode) {
                jsonData = item;
                return true;
            }
        });
    }
    return jsonData;
}

//根据地区编码获取地区名
export function getNamebyAdcode(adcode) {
    const lastFour = String(adcode).slice(-4);
    const lastTwo = String(adcode).slice(-2);
    let name;
    //省
    if (lastFour === '0000') {
        return province.features[0].properties.name;
    }
    //市
    else if (lastFour != '0000' && lastTwo === '00') {
        city.features.some((item) => {
            if (item.properties.adcode == adcode) {
                name = item.properties.name;
                return true;
            }
        });
    }
    //县
    else {
        county.features.some((item) => {
            if (item.properties.adcode == adcode) {
                name = item.properties.name;
                return true;
            }
        });
    }
    return name;
}

//根据地区名获取地区编码
export function getAdcodebyName(name) {
    let adcode;
    //省
    if (name.includes('省')) {
        return province.features[0].properties.adcode;
    }
    //市
    city.features.some((item) => {
        if (item.properties.name == name) {
            adcode = item.properties.adcode;
            return true;
        }
    });
    if (adcode) {
        return adcode;
    }
    //县
    county.features.some((item) => {
        if (item.properties.name == name) {
            adcode = item.properties.adcode;
            return true;
        }
    });
    return adcode;
}

//获取下拉option
export function getOptions() {
    let arr = [
        {
            label: getNamebyAdcode(county.features[0].properties.acroutes[1]),
            value: county.features[0].properties.acroutes[1],
            children: []
        }
    ];

    let cityArr = []; //存放已有的市编码

    county.features.forEach((element) => {
        if (
            cityArr.some((adcode) => {
                return adcode === element.properties.parent.adcode;
            })
        ) {
            arr[0].children.forEach((item) => {
                if (item.value === element.properties.parent.adcode) {
                    item.children.push({
                        label: element.properties.name,
                        value: element.properties.adcode
                    });
                }
            });
        } else {
            cityArr.push(element.properties.parent.adcode);
            arr[0].children.push({
                label: getNamebyAdcode(element.properties.parent.adcode),
                value: element.properties.parent.adcode,
                children: [
                    {
                        label: element.properties.name,
                        value: element.properties.adcode
                    }
                ]
            });
        }
    });
    return arr;
}

//获取市，用于添加市边界线
export function getAllCity() {
    return city;
}
